package com.example.easyscript.controller;

import com.example.easyscript.bean.task.StatusBean;
import com.example.easyscript.bean.task.TaskBean;
import com.example.easyscript.dto.Req.AddTaskReq;
import com.example.easyscript.service.SystemImageServive;
import com.example.easyscript.service.master.MasterServiceImpl;
import com.example.easyscript.service.master.intf.MasterService;
import com.example.easyscript.service.master.intf.TaskManagerService;
import com.example.easyscript.utils.ImageUtils;
import com.example.easyscript.utils.JnaUtils;
import com.example.easyscript.utils.RobotUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

@Api(value = "TaskController", tags = "测试")
@Slf4j
@RestController
@RequestMapping(value = "/web/test")
public class TestController {
    @Resource
    private MasterServiceImpl masterService;

    @Resource
    private TaskManagerService taskManagerService;

    @Resource
    private SystemImageServive systemImageServive;

    private Integer runCount = 0;

    @PostMapping("/clean")
    @ApiOperation("清除运行列表")
    public void clean() {
        taskManagerService.cleanAll();
        runCount = 0 ;
    }

    /**
     * 测试一 、从数据库里面加载对应的任务模型
     */
    @ApiOperation("加载测试任务模型")
    @PostMapping("/contextLoad/{taskId}/{operationId}")
    public void contextLoad(@PathVariable Integer operationId, @PathVariable Integer taskId) {
        masterService.initContext();
        AddTaskReq addTaskReq = new AddTaskReq();
        addTaskReq.setTaskId(taskId);
        addTaskReq.setOperationList(Arrays.asList(operationId));
        taskManagerService.addTask(addTaskReq);
    }

    /**
     * 测试二、通过加载的任务模型拿到阴阳师的庭院状态
     * 并对该状态进行检测，测试时现将软件切到庭院状态
     */
    @ApiOperation("图片识别测试")
    @PostMapping("/imageReconginze")
    public void testCapture() {
        //加载一个任务模型
        TaskBean taskBean = null;
        try {
            taskBean = taskManagerService.getNextTask();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //通过任务模型对该任务进行截图
        String capture = systemImageServive.getTaskCurrentCapture(taskBean);
        //拿到所有的状态
        Map<Integer, StatusBean> stateBeanMap = taskBean.getStateDiagram().getStateBeanMap();
        StatusBean test = stateBeanMap.get(1);
        systemImageServive.checkTemplateIsInSource(test.getSignsUrl(),capture);
    }

    /**
     * 测试三、完整跑一次运行流程
     */
    @ApiOperation("单次运行")
    @PostMapping("/runContext")
    public void runContext() {
        System.out.println("单步运行次数 ： !!!!"+ (++runCount) +"  !!!!");
        masterService.testRun();
    }

    /**
     * 测试四、组合测试 加载一次任务模型跑两次
     */
    @ApiOperation("加载一次任务模型跑两次")
    @PostMapping("/runContextTwo")
    public void runContextTwo() {
        runContext();
    }

    @ApiOperation("加载任务并且运行多次")
    @PostMapping("/autoSearch/{time}")
    public void runAuto(@PathVariable Integer time){
        System.out.println("尝试运行一次打御魂任务");
        for (int i = 0; i < time; i++) {
            runContext();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
